package genedit.controller.interfaceActions;

import genedit.model.Workspace;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Point;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.HashMap;

import javax.swing.JFrame;

/**
 * Klasa zadužena za formiranje datoteke u kojoj se čuvaju informacije
 * podešavanja izgleda GUIa aplikacije pre njenog gašenja.
 * 
 * @author Voja
 * @version 1.5 15/12/13
 */
public class PropertyManager implements Serializable {
	private static final long serialVersionUID = 1L;

	public static final String WorkspacePath = "WorkspacePath";
	public static final String ToolbarPosition = "ToolbarPosition";
	public static final String LayoutPath = "LayoutPath";
	public static final String MainFrameDimension = "MainFrameDimension";
	public static final String MainFrameLocation = "MainFrameLocation";
	public static final String MainFrameExtendedState = "MainFrameExtendedState";
	public static final String ProjectTreeVisible = "ProjectTreeVisible";
	public static final String GridVisible = "GridVisible";
	public static final String AutoLinkOptimize = "AutoLinkOptimize";

	private static PropertyManager instance = null;
	private HashMap<String, Object> propertyMap;
	protected static final String filename = "properties.dat";

	public Object getProperty(String propertyName) {
		return propertyMap.get(propertyName);
	}

	public void setProperty(String propertyName, Object newProperty) {
		propertyMap.put(propertyName, newProperty);
	}

	protected PropertyManager() {
		propertyMap = new HashMap<String, Object>();
		if (propertyFileExists()) {
			try {
				load();
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			initManager();
		}
	}

	/**
	 * Sačuvavanje datoteke sa informacijama o vrednostima propertia. Izvršava
	 * se prilikom zatvaranja aplikacije.
	 */
	public void save() throws FileNotFoundException, IOException {
		propertyMap.put(WorkspacePath, Workspace.getInstance().getPath().getCanonicalFile());
		File file = new File(filename);
		ObjectOutputStream stream = new ObjectOutputStream(new FileOutputStream(file));
		stream.writeObject(propertyMap);
		stream.close();
	}

	/**
	 * Učivatavanje datoteke ukoliko ona postoji. Ovo se izvršava prilikom
	 * pokretanja aplikacije.
	 */
	@SuppressWarnings("unchecked")
	public void load() throws FileNotFoundException, IOException, ClassNotFoundException {
		File file = new File(filename);
		ObjectInputStream stream = new ObjectInputStream(new FileInputStream(file));
		propertyMap = (HashMap<String, Object>) stream.readObject();
		stream.close();
		Workspace.getInstance().setPath((File) propertyMap.get(PropertyManager.WorkspacePath));
	}

	public boolean propertyFileExists() {
		File file = new File(filename);
		return file.exists();
	}

	/**
	 * Inicijalizacija menadžera propertia.
	 */
	public void initManager() {
		propertyMap.put(WorkspacePath, ".\\");
		Workspace.getInstance().setPath(new File(".\\"));

		propertyMap.put(ToolbarPosition, BorderLayout.NORTH);
		propertyMap.put(LayoutPath, "layout.dat");
		propertyMap.put(MainFrameDimension, new Dimension(800, 600));
		propertyMap.put(MainFrameLocation, new Point(0, 0));
		propertyMap.put(MainFrameExtendedState, new Integer(JFrame.MAXIMIZED_BOTH));
		propertyMap.put(ProjectTreeVisible, new Boolean(true));
		propertyMap.put(GridVisible, new Boolean(false));
		propertyMap.put(AutoLinkOptimize, new Boolean(false));
	}

	public static PropertyManager getInstance() {
		if (instance == null)
			instance = new PropertyManager();
		return instance;
	}
}